//
//  MenuTool.swift
//  Runner
//
//  Created by mini2025 on 2025/10/11.
//

import Foundation
import Cocoa

class MenuTool{
    
    static let shared = MenuTool.init()
    
    var statusItem: NSStatusItem!
    
    func createMenu(){
        // 1️⃣ 创建状态栏图标
        statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
        if let button = statusItem.button {
            if #available(macOS 11.0, *) {
                button.image = NSImage(systemSymbolName: "star.fill", accessibilityDescription: "App Icon")
            } else {
                // Fallback on earlier versions
            }
            button.action = #selector(statusBarButtonClicked)
            button.target = self
        }
        
        let openItem = NSMenuItem(title: "打开主界面", action: #selector(openMainApp), keyEquivalent: "")
        openItem.target = self  // 👈 关键：设置 target

        let copyItem = NSMenuItem(title: "翻译-从粘贴板读取", action: #selector(copyPaste), keyEquivalent: "")
        copyItem.target = self  // 👈 关键

        let quitItem = NSMenuItem(title: "退出", action: #selector(quitApp), keyEquivalent: "q")
        quitItem.target = self  // 👈 关键（可选，但推荐）
        
        // 2️⃣ 创建菜单
        let menu = NSMenu()
        menu.addItem(openItem)
        menu.addItem(NSMenuItem.separator())
        menu.addItem(copyItem)
        menu.addItem(NSMenuItem.separator())
        menu.addItem(quitItem)
        
        // 3️⃣ 绑定菜单
        statusItem.menu = menu
    }
    
    
    @objc func statusBarButtonClicked() {
        
    }
    
    @objc func copyPaste() {
        AppBridgeTool.shared.callFunc(method: "createTranslateDataCopy", arguments: nil, results: nil)
    }

    @objc func openMainApp() {
        print("打开主界面")
        NSApp.activate(ignoringOtherApps: true)
    }

    @objc func quitApp() {
        NSApp.terminate(nil)
    }
}
